package dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import tangyaxin.www.saoerweima.R;

public class ChangeColorWindow extends PopupWindow {
    private Context context;
    private View layoutView,windowView,backgroundTab,foregronudTab;
    private GridView paletteGV ;
    private View closeBtn;
    private boolean isBackground = true;
    public ChangeColorWindow(Context context){
        this.context = context;
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutView = layoutInflater.inflate(R.layout.dialog_change_color_window,null);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(layoutView);
        findView();
        initView();
    }

    private void findView(){
        backgroundTab = layoutView.findViewById(R.id.background_tab);
        foregronudTab = layoutView.findViewById(R.id.foreground_tab);
        windowView = layoutView.findViewById(R.id.window_view);
        paletteGV = (GridView) layoutView.findViewById(R.id.palette);
        closeBtn = layoutView.findViewById(R.id.close_btn);
    }

    private void initView(){
        backgroundTab.setOnClickListener(onClickListener);
        foregronudTab.setOnClickListener(onClickListener);
        closeBtn.setOnClickListener(onClickListener);

        int[] colorArray = {
                R.color.palette_color_11,R.color.palette_color_12,R.color.palette_color_13,R.color.palette_color_14,R.color.palette_color_15,
                R.color.palette_color_21,R.color.palette_color_22,R.color.palette_color_23,R.color.palette_color_24,R.color.palette_color_25,
                R.color.palette_color_31,R.color.palette_color_32,R.color.palette_color_33,R.color.palette_color_34,R.color.palette_color_35,
                R.color.palette_color_41,R.color.palette_color_42,R.color.palette_color_43,R.color.palette_color_44,R.color.palette_color_45,
                R.color.palette_color_51,R.color.palette_color_52,R.color.palette_color_53,R.color.palette_color_54,R.color.palette_color_55,
                R.color.palette_color_61,R.color.palette_color_62,R.color.palette_color_63,R.color.palette_color_64,R.color.palette_color_65
        };
        paletteGV.setAdapter(new ColorAdapter(context,colorArray));
    }

    public void show(View parent){
        showAtLocation(parent, Gravity.BOTTOM,0, (int) (context.getResources().getDisplayMetrics().density * 90));
    }
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.background_tab:
                    isBackground = true;
                    windowView.setBackgroundResource(R.mipmap.palette_page_left);
                    break;

                case R.id.foreground_tab:
                    isBackground = false;
                    windowView.setBackgroundResource(R.mipmap.palette_page_right);
                    break;

                case R.id.close_btn:
                    dismiss();
                    break;
            }
        }
    };


    private class ColorAdapter extends BaseAdapter {
        private Context context;
        private int[] colorArray;
        private ImageView bgPreIV,fgPreIV;

        public ColorAdapter(Context context,int[] colorArray){
            this.context = context;
            this.colorArray = colorArray;
        }

        @Override
        public int getCount() {
            return colorArray.length;
        }

        @Override
        public Integer getItem(int position) {
            return colorArray[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView imageView = null;
            if(convertView == null){
                convertView = View.inflate(context, R.layout.item_color_block,null);
                imageView = (ImageView) convertView;
                convertView.setTag(imageView);
            } else {
                imageView = (ImageView) convertView.getTag();
            }
            imageView.setImageResource(getItem(position));
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if(bgPreIV == null){
//                        bgPreIV = finalImageView;
//                    } else {
//                        bgPreIV.setEnabled(true);
//                    }
//                    finalImageView.setEnabled(false);

                    if(bgPreIV != null) bgPreIV.setEnabled(true);
                    if(fgPreIV != null) fgPreIV.setEnabled(true);
                    ImageView iv = ((ImageView)v);
                    int color = ((ColorDrawable)iv.getDrawable()).getColor();
                    if(isBackground){
                        iv.setEnabled(false);
                        bgPreIV = iv;
                        if(onColorChangedListener != null) onColorChangedListener.onBgColorChanged(color);
                    } else {
                        iv.setEnabled(false);
                        fgPreIV = iv;
                        if(onColorChangedListener != null) onColorChangedListener.onFgColorChanged(color);
                    }
                }
            });
            return convertView;
        }
    }

    private OnColorChangedListener onColorChangedListener;

    public void setOnColorChangedListener(OnColorChangedListener onColorChangedListener) {
        this.onColorChangedListener = onColorChangedListener;
    }

    public interface OnColorChangedListener {
        void onBgColorChanged(int color);
        void onFgColorChanged(int color);
    }
}
